package com.training.food.controller;

import com.alibaba.fastjson.JSON;
import com.training.food.Mapper.InitialpurMapper;
import com.training.food.Mapper.SlaughterMapper;
import com.training.food.Mapper.WSmarketMapper;
import com.training.food.pojo.*;
import com.training.food.service.CheckService;
import com.training.food.service.InitialpurService;
import com.training.food.service.SlaughterService;
import com.training.food.service.WSmarketService;
import com.training.food.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class WSmarketController {
    @Autowired
    SlaughterMapper slaughterMapper;
    @Resource
    SlaughterService slaughterService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    //增加批发市场信息
    @PostMapping("/food/buy/addWSmarket")
    public CommonResult AddWSmarket(@RequestBody Slaughter slaughter, HttpServletRequest request){
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;

        Slaughter slaughter1=slaughterService.selectById(slaughter.getSlaughterId());
        slaughter1.setNumber(slaughter1.getNumber()-slaughter.getNumber());
        if(slaughter1.getNumber()==0){
            slaughter1.setState("3");
        }
        slaughterService.update(slaughter1);

        Initialpur initialpur=new Initialpur();
        initialpur.setSlaughterId(slaughter1.getSlaughterId());

        WSmarket wSmarket=new WSmarket();
        wSmarket.setInputTime(new Date());
        wSmarket.setUsername(user.getUsername());
        wSmarket.setState("0");
        wSmarket.setNumber(slaughter.getNumber());
        wSmarket.setTypeDescription("*");
        wsmarketService.add(wSmarket);

        initialpur.setWSmarketId(wSmarket.getWSmarketId());

        initialpurSerice.add(initialpur);
        return CommonResult.success("成功了");

    }

    @PostMapping("/food/buy/selectWSmarket")
    //查询批发市场信息
    public CommonResult selectWSmarket(HttpServletRequest request, @RequestBody WSmarket wsmarket) throws UnsupportedEncodingException {
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        if(user==null){
            CommonResult.failed("用户没有找到");
        }
        List<WSmarket> lists=wsmarketService.selectByUser(user.getUsername(),wsmarket.getState());
        if (lists.size()>0){
            return CommonResult.success(lists);
        }
        return CommonResult.failed("没有找到");
    }
    @PostMapping("/food/buy/selectAllWSmarket")
    //查询所有批发市场信息
    public CommonResult selectAllWSmarket(HttpServletRequest request, @RequestBody WSmarket wsmarket) throws UnsupportedEncodingException{

        List<WSmarket> lists=wsmarketService.selectAll(wsmarket);
        if (lists.size()>0){
            return CommonResult.success(lists);
        }
        return CommonResult.failed("没有找到");
    }
    @Autowired
    InitialpurMapper initialpurMapper;
    @Autowired
    WSmarketMapper wSmarketMapper;
    @Resource
    WSmarketService wsmarketService;
    @Resource
    InitialpurService initialpurSerice;
    @Resource
    CheckService checkService;
    //更改批发市场信息
    @PostMapping("/food/buy/updateWSmarketInfo")
    public CommonResult updateWSmarketInfo(HttpServletRequest request, @RequestBody Map<String,Object> req) {
        Map<String,String>ws= (Map<String, String>) req.get("WSmarket");
        WSmarket wsmarket= JSON.parseObject(JSON.toJSONString(ws),WSmarket.class);
        int flag=wsmarket.getWSmarketId();
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        WSmarket wSmarket1= wSmarketMapper.select( wsmarket);
        wsmarket.setState(wSmarket1.getState());
        wsmarket.setInputTime(wSmarket1.getInputTime());
        wsmarket.setUsername(wSmarket1.getUsername());
        int tmp=wsmarketService.add(wsmarket);
        Initialpur initialpur=new Initialpur();
        initialpur.setWSmarketId(flag);
        //System.out.println(initialpur);
        Initialpur initialpur1=initialpurMapper.select(initialpur);

        initialpur.setWSmarketId(tmp);
        initialpur.setSlaughterId(initialpur1.getSlaughterId());
        initialpurSerice.add(initialpur);

        return  CommonResult.success("成功了");
    }
    //获取所有为空的批发市场信息
    @PostMapping("/food/buy/selectAllMarketNull")
    public CommonResult selectAllMarketNUll(HttpServletRequest request) {

        return(CommonResult.success(wsmarketService.selectByNull())) ;
    }
    //更改批发市场交易状态
    @PostMapping("/food/buy/updateWSmarket")
    public CommonResult updateWSmarket(HttpServletRequest request, @RequestBody WSmarket wsmarket){
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        wsmarket.setUsername(user.getUsername());
        WSmarket wsmarket1=wSmarketMapper.select(wsmarket);
        wsmarket1.setState(wsmarket.getState());
        wsmarketService.update(wsmarket1);

        return CommonResult.success("增加批发市场信息成功");
    }
    //删除批发市场信息
    @PostMapping("/food/buy/deleteWSmarket")
    public CommonResult DeleteWSmarket(HttpServletRequest request,@RequestBody Map<String,Object> reqs){
        Map<String,String>ws=(Map<String,String>)reqs.get("wsmarket");
        Map<String,String>ini=(Map<String,String>)reqs.get("initialpur");
        Map<String,String>ch=(Map<String,String>)reqs.get("check");
        WSmarket wsmarket=JSON.parseObject(JSON.toJSONString(ws), WSmarket.class);
        Initialpur initialpur=JSON.parseObject(JSON.toJSONString(ini), Initialpur.class);
        Check check=JSON.parseObject(JSON.toJSONString(ch), Check.class);
        if(wsmarketService.select(wsmarket)){
            wsmarketService.sub(wsmarket);

            initialpurSerice.sub(initialpur);
            if(checkService.select(check)){
                checkService.sub(check);
            }
        }

        return CommonResult.success("删除批发市场信息成功");
    }
}
